var zTree, rMenu, code;
$(function() {
    var setting = {
        view: {
            dblClickExpand: false,
            showLine: false,
            selectedMulti: false
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: ""
            }
        },
        callback: {
            beforeClick: function (treeId, treeNode) {
                $.fn.zTree.getZTreeObj("treeDemo");
                if (treeNode.isParent) {
                    $.fn.zTree.getZTreeObj("treeDemo").expandNode(treeNode);
                    $("#menuId").val(treeNode.id);
                    $("#menuPid").val(treeNode.pId);
                    $("#menuName").val(treeNode.name);
                    $("#url").val(treeNode.path);
                    $("#remarks").val(treeNode.remarks);
                    return false;
                } else {
                    $("#menuId").val(treeNode.id);
                    $("#menuPid").val(treeNode.pId);
                    $("#menuName").val(treeNode.name);
                    $("#url").val(treeNode.path);
                    $("#remarks").val(treeNode.remarks);
                    return true;
                }
            },
            onRightClick: OnRightClick
        }
    };
    $(document).ready(function(){
        $.ajax({
            method : "POST",
            dataType : "json",
            url : "/role/selectTreeMenu",
            success : function(data){
                var  result = eval(data);
                if(result.success == true){
                    var zNodes = result.data;
                    zTree = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                    rMenu = $("#rMenu");
                }else{
                    layer.msg(result.msg,{icon:2,time:1000});
                }
            }
        })
    });
});

function showCode(str) {
    if (!code) code = $("#code");
    code.empty();
    code.append("<li>"+str+"</li>");
}

// function OnRightClick(event, treeId, treeNode) {
//     if (event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 1 && treeNode.pId =='') {
//         zTree.selectNode(treeNode);
//         showRMenu("root", event.clientX, event.clientY);
//     }else if( event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 1 && treeNode.pId =='01'){
//         zTree.selectNode(treeNode);
//         showRMenu("sonRoot", event.clientX, event.clientY);
//     }else if (treeNode && !treeNode.noR) {
//         zTree.selectNode(treeNode);
//         showRMenu("grandSonRoot", event.clientX, event.clientY);
//     }
// }

function OnRightClick(event, treeId, treeNode) {
    if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
        zTree.cancelSelectedNode();
        showRMenu("root", event.clientX, event.clientY);
    } else if (treeNode && !treeNode.noR) {
        if(treeNode.pId ==''){
            zTree.selectNode(treeNode);
            showRMenu("root", event.clientX, event.clientY);
        }else if(treeNode.pId =='01'){
            zTree.selectNode(treeNode);
            showRMenu("sonRoot", event.clientX, event.clientY);
        }else{
            zTree.selectNode(treeNode);
            showRMenu("grandSonRoot", event.clientX, event.clientY);
        }
    }
}

function showRMenu(type, x, y) {
    $("#rMenu ul").show();
    if (type=="root") {
        $("#m_del").hide();
        $("#m_update").hide();
        $("#m_add").show();
    }else if(type=="sonRoot"){
        $("#m_del").show();
        $("#m_update").show();
        $("#m_add").show();
    } else if(type=="grandSonRoot"){
        $("#m_add").hide();
        $("#m_del").show();
        $("#m_update").show();
    }

    y += document.body.scrollTop;
    x += document.body.scrollLeft;
    rMenu.css({"top":y+"px", "left":x+"px", "visibility":"visible"});

    $("body").bind("mousedown", onBodyMouseDown);
}
function hideRMenu() {
    if (rMenu) rMenu.css({"visibility": "hidden"});
    $("body").unbind("mousedown", onBodyMouseDown);
}
function onBodyMouseDown(event){
    if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length>0)) {
        rMenu.css({"visibility" : "hidden"});
    }
}
function addTreeNode() {
    hideRMenu();
    menu_add('添加菜单','/menu/addMenuInfo?Id='+zTree.getSelectedNodes()[0].id+'','800','300');
}
function removeTreeNode() {
    hideRMenu();
    var nodes = zTree.getSelectedNodes();
    if (nodes && nodes.length>0) {
         menu_del(zTree.getSelectedNodes()[0].id);
    }
}
function updateTreeNode(checked) {
    hideRMenu();
    var nodes = zTree.getSelectedNodes();
    if (nodes && nodes.length>0) {
        zTree.checkNode(nodes[0], checked, true);
        menu_edit('菜单编辑','/menu/updateMenuInfo?Id='+zTree.getSelectedNodes()[0].id+'',zTree.getSelectedNodes()[0].id,'800','300');
    }
}
/*添加*/
function menu_add(title,url,w,h){
    layer_show(title,url,w,h);
}
/*编辑*/
function menu_edit(title,url,id,w,h){
    layer_show(title,url,w,h);
}
/*删除*/
function menu_del(Id){
    var params = {Id:Id};
    layer.confirm('确认要删除吗？',function(index){
        $.ajax({
            type: 'POST',
            url: '/menu/deleteMenuById',
            data: params,
            dataType: 'json',
            success: function(data){
                var data = eval(data);
                if (data.success == true) {
                    layer.msg(data.msg,{icon:1,time:2000},function(){
                        window.location.reload();
                    });
                } else if(data.success == false){
                    layer.msg(data.msg,{icon:2,time:4000});
                    return;
                }
            },
            error:function(data) {
                console.log(data.msg);
            },
        });
    });
}